package xiaohu.day4.HJ43;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static int m;
    static int n;
    static int[][] graph;
    static int[][] offsets = new int[][]{{-1,0},{1,0},{0,1},{0,-1}};
    static int[] visited;
    static List<int[]> path;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        graph = new int[m][n];
        path = new ArrayList<>();

        visited = new int[m*n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = sc.nextInt();
            }
        }
        visited[0] = 1;
        if (dfs(0,0)) {
            for (int[] point : path) {
                System.out.println("(" + point[0] + "," + point[1] + ")");
            }
        }
    }

    private static boolean dfs(int i, int j) {
        path.add(new int[]{i,j});
        if (i==m-1 && j==n-1) {
            return true;
        }

        for (int[] offset : offsets) {
            int newX = i+offset[0];
            int newY = j+offset[1];
            int pos = newX * n + newY;

            if (newX<0 || newX>=m || newY<0 || newY>=n
                    || visited[pos]==1 || graph[newX][newY]==1) {
                continue;
            }
            visited[pos] = 1;
            if (dfs(newX, newY)) {
                return true;
            }
        }
        path.remove(path.size()-1);
        return false;
    }
}

